package com.ld;

import com.ld.entity.PageEntity;
import com.ld.example.User;
import com.ld.util.PageUtil;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author: ld
 * @Date: 2021-02-02 17:14
 */
public class test {

    String[] strings = new String[]{"a","av","cc","ac","ad","xd","va","bc","de","dx"};
    List<User> init(){
        List<User> list = new ArrayList<>();
        for (int i=0;i<strings.length;i++){
            int max=100,min=1;
            int random = (int) (Math.random()*(max-min)+min);
            User user = new User();
            user.setId(i);
            user.setName(strings[i]);
            user.setAge("age_"+strings[i]);
            user.setHome("home_"+strings[i]);
            user.setSchool("school_"+strings[i]);

            user.setTelephone(random+"");
            list.add(user);
            System.out.println("初始数据："+user);

        }
        System.out.println("-----------------------------------------------------");
        return list;
    }

    /**
     * 测试统一分页处理
     */
    @Test
    public void testPageEntity(){
        List<User> userList = init();
        PageEntity<User> pageEntity = new PageEntity<>();
        pageEntity.setPageNum(1);
        pageEntity.setPageSize(5);
        pageEntity.setOrderBy("id");
        pageEntity.setSortBy("desc");
        User user = new User();
        user.setId(5);
        //1、查询列表中所有符合条件的数据
        List<User> byEntityFilter = PageUtil.getByEntityFilter(user, userList);
        //2、将符合筛选条件的数据进行分页和排序
        PageEntity<List<User>> listPageEntity = PageUtil.basePageEntity(pageEntity, byEntityFilter);
        listPageEntity.getData().forEach(System.out::println);
    }

    /**
     * 测试根据实体类的条件筛选数据
     */
    @Test
    public  void testEntityFilter(){
        List<User> inList = init();

        User user = new User();
        //设置筛选条件，可设置多个条件
        user.setAge("age_av");
        //user.setHome("home_xd");
        //根据条件从list中查询回具体数据
        List<User> users = PageUtil.getByEntityFilter(user, inList);
        users.forEach(System.out::println);
    }

    /**
     * 测试分页排序
     */
    @Test
    public  void testDivisionList(){
        List<User> inList = init();
        int pageNum = 2;
        int pageSize = 3;
        String orderBy="id";
        String sortBy = "asc";
        //根据条件从list中查询回具体数据
        List<User> users = PageUtil.divisionList(inList,pageNum,pageSize,orderBy,sortBy);
        users.forEach(System.out::println);
    }

    /**
     * 测试排序
     */
    @Test
    public  void testSort(){
        List<User> inList = init();
        String orderBy="telephone";
        String sortBy = "desc";
        //根据条件从list中查询回具体数据
        PageUtil.sort(inList,orderBy,sortBy);
        inList.forEach(System.out::println);
    }
}
